import { get } from './base'

export function processSongs(songs:any) {
   if (!songs.length) {
    return Promise.resolve(songs)
  }
  // 批量获取歌曲的播放url
  /*return get('/api/getSongsUrl', {
    mid: songs.map((song:any) => {
      return song.mid
    })
  }).then((result) => {
    const map = result.map

    return songs.map((song:any) => {
      song.url = map[song.mid]
      return song
    }).filter((song:any) => {
        return song.url && song.url.indexOf('vkey') > -1
    })
  })*/
   return new Promise((resolve) => {
    const data = songs.map((song:any) => {
        song.url = '/mp3/qintian.mp3' 
        return song
    })
     resolve(data)
    })
}

const lyricMap = {} as any

export function getLyric(song:any) {
  if (song.lyric) {
    return Promise.resolve(song.lyric)
  }
  const mid = song.mid
  const lyric = lyricMap[mid] // 使用对象作为缓存
  if (lyric) {
    return Promise.resolve(lyric)
  }

  return get('/api/getLyric', {
    mid
  }).then((result) => {
    const lyric = result ? result.lyric : '[00:00:00]该歌曲暂时无法获取歌词'
    lyricMap[mid] = lyric
    return lyric
  })
}
